#include "../../Command.h"

#include "../../User.h"
#include "../../NLogic/Static.h"
#include "../../NLogic/Battle.h"

namespace ProjectZ { namespace NCommand {

void Command::CS_REQ_DUNGEON_RANDOM_PARTY(User * pUser, ::Network::Packet *pPacket)
{
	/*
	 * /// 던전 빠른 파티 검색 요청
	CS_REQ_DUNGEON_RANDOM_PARTY		0x1118
	{
	}
	SC_REQ_DUNGEON_RANDOM_PARTY		0x1119
	{
		s16	ack;					/// 1:완료 ,  -1 실패
		u32	party_seq;				/// 파티 시퀀스
	}
	 */

#if 0
	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	* pAck << static_cast<int16_t>(ACK_UNKNOWN_ERROR);
	pAck->MakePacket(CMD_SC_REQ_DUNGEON_RANDOM_PARTY);
	pUser->Send(pAck);

	DEBUGLOG("CS_REQ_DUNGEON_RANDOM_PARTY pUser->GetEnumState() != User::EnumState::__NONE__");
	return;
#endif

	NLogic::Space * pWorld = pUser->GetSpace();
	if (NULL == pWorld)
	{
		DEBUGLOG("CS_REQ_DUNGEON_RANDOM_PARTY NULL == pWorld");
		return;
	}

	if (false == pUser->EndBattle_afterEffect())
	{
		::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
		* pAck << static_cast<int16_t>(ACK_BATTLE_AFTEREFFECT) << static_cast<uint32_t>(0);
		pAck->MakePacket(CMD_SC_REQ_DUNGEON_RANDOM_PARTY);
		pUser->Send(pAck);
		STRESSLOG("CS_REQ_DUNGEON_CREATE_SINGLE ACK");

		return;
	}

	if (pUser->GetEnumState() != User::EnumState::__NONE__)
	{
		::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
		* pAck << static_cast<int16_t>(ACK_UNKNOWN_ERROR);
		pAck->MakePacket(CMD_SC_REQ_DUNGEON_RANDOM_PARTY);
		pUser->Send(pAck);

		DEBUGLOG("CS_REQ_DUNGEON_RANDOM_PARTY pUser->GetEnumState() != User::EnumState::__NONE__");
		return;
	}

	PREPARE_PACKET;

	pWorld->LeaveUser(pUser);

	NLogic::Party * pParty = NLogic::Static::Instance().GETPARTYPOOL()->NEW();
	pParty->Open(pUser);
	pParty->SetState(NLogic::Party::State::INVITE);

	NLogic::Space::Data clsData;
	clsData._maxUser = 3;
	clsData._spaceType = NLogic::Space::SpaceType::PARTY_DUNGEION;

	NLogic::Space * pPartyDungeon = NLogic::Static::Instance().GETSPACEPOOL()->NEW();
	pPartyDungeon->Open(pUser, clsData);
	pPartyDungeon->EnterUser(pUser);

	pPartyDungeon->SetParty(pParty);

	//pUser->player_info.DungeonInfo.party_seq = pPartyDungeon->GetData()._index;
	pUser->GetBattle()->GetData()._combo_steak = 0;
	pUser->SetEnumState(User::EnumState::RANDOM_PARTY_WAIT);

	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	* pAck << static_cast<int16_t>(ACK_OK) << pPartyDungeon->GetData()._index;
	pAck->MakePacket(CMD_SC_REQ_DUNGEON_RANDOM_PARTY);
	pUser->Send(pAck);
}

} /*NCommand*/ } /*ProjectZ*/
